<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //现有字符串如下 name1与value1为一组参数信息 name1表示为参数名value1表示为值 
        // 1、 从search字符串中 提取出name1参数的值  值为value1
        var search = '?name1=value1&name2=value2';


        // 2、封装一个函数可以获取到指定参数的值
        var num = 0;
        function getUrlParams(key, str) {
            var newsearch = str.replace("?", "");
            newsearch = newsearch.split("&");
            // console.log( newsearch);
            // console.log(newsearch[0])
            newsearch.forEach(function() {
                var obj = newsearch[num].split("=");
                num++
                // console.log(obj);
                if(obj[0]==key){
                    console.log(obj[1])
                }
            });
        }
        getUrlParams('name2', search)
    </script>
</body>

</html>